exec(open("import_qt.py").read())

class Recorder:
        def __init__(self, buf, log, stList, par = None):
                self.buf = buf #iodevice for sample storage
                self.log = log #log function
                self.par = par #parent qt object for technical reasons
                self.stList = stList #status litener list

        def startRecord(self, dev):
                format = QAudioFormat()
                format.setSampleRate(8000);
                format.setChannelCount(1);
                format.setSampleSize(8);
                format.setCodec("audio/pcm");
                format.setByteOrder(QAudioFormat.BigEndian);
                format.setSampleType(QAudioFormat.UnSignedInt);

                if(dev>=0):
                        info = QAudioDeviceInfo.availableDevices(QAudio.AudioInput)[dev]
                else:
                        info = QAudioDeviceInfo(QAudioDeviceInfo.defaultInputDevice())
                        
                if (not(info.isFormatSupported(format))) :
                        format = info.nearestFormat(format);
                self.audio = QAudioInput(format, self.par);
                self.audio.stateChanged.connect(self.stateChanged)
                self.audio.start(self.buf);
                if(self.audio.state()!=QAudio.ActiveState):
                        self.log("State not active")
                else:
                        self.log("Recording with "+info.deviceName())

        def stopRecord(self):
                if(not(self.audio)):
                  return
                self.audio.stop();
                del self.audio

        def deviceList(self):
                info = QAudioDeviceInfo(QAudioDeviceInfo.defaultInputDevice())
                list = []; 
                for dev in info.availableDevices(QAudio.AudioInput):
                        list.append(dev.deviceName())

                return list
                
        def stateChanged(self, status):
                map = {0 : "Active",
                       1 : "Suspended",
                       2 : "Stopped",
                       3 : "Idle"}
                print("Status", map[status])
                for l in self.stList:
                        l(status)
